package com.xrit.factory;


import com.xrit.inter.Car;
import com.xrit.pojo.Benchi;
import com.xrit.pojo.Falali;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class CarFactory {
    public  static Car createCar(String name){
        if ("Benchi".equals(name)){
            return  new Benchi();
        }else if ("Falali".equals(name)){
            return new Falali();
        }
        return null;
    }

    public  static  Car getIne(String name) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
        ClassLoader classLoader = CarFactory.class.getClassLoader();
        //加载器获取输入流，读取pro.properties文件
        InputStream inputStream = classLoader.getResourceAsStream("my.properties");
        Properties properties = new Properties();
        //集合IO关联
        properties.load(inputStream);
        //获取集合中的键值对，类名
        String className = properties.getProperty("className");

        if (className.equals(name)){
            return  che(name);
        }else if (className.equals(name)){
            return   che(name);
        }
        return null;

    }

    public   static   Car  getppp(String name) throws Exception {
        ClassLoader classLoader = CarFactory.class.getClassLoader();
        //加载器获取输入流，读取pro.properties文件
        InputStream inputStream = classLoader.getResourceAsStream("my.properties");
        Properties properties = new Properties();
        //集合IO关联
        properties.load(inputStream);
        //获取集合中的键值对，类名
        String className = properties.getProperty("className");
        System.out.println(className);

        if (className.equals(name)){
            return  che(name);
        }else if (className.equals(name)){
            return   che(name);
        }
        return null;

    }
    public static Car che(String name) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class<?> c = Class.forName(name);
        Car car = (Car) c.newInstance();
        return  car;
    }


}
